﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;

namespace ETLContactBook.Utils
{
    public static class SerializationUtils
    {
        public static bool SerializeToFile<T>(T value, string file) where T : class { try { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); TextWriter textWriter = new StreamWriter(file); XmlTextWriter writer = new XmlTextWriter(textWriter); serializer.WriteObject(writer, value); textWriter.Close(); writer.Close(); return true; } catch { return false; } }
        public static T DeserializeFromFile<T>(string file) where T : class
        {
            try { DataContractSerializer ser = new DataContractSerializer(typeof(T)); StreamReader stream = new StreamReader(file); XmlTextReader reader = new XmlTextReader(stream); var objet = ser.ReadObject(reader, false); reader.Close(); stream.Close(); return (T)objet; }
            catch (Exception ex) { throw ex; }
        }
    }
}